package com.glq1218.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glq1218.article.mapper.ArticleMapper;
import com.glq1218.report.bo.ReportBO;
import com.glq1218.report.entity.Report;
import com.glq1218.domain.*;
import com.glq1218.report.bo.AddReportBO;
import com.glq1218.report.bo.QueryReportBO;
import com.glq1218.report.service.ReportService;
import com.glq1218.report.mapper.ReportMapper;
import com.glq1218.user.entity.User;
import com.glq1218.user.mapper.UserMapper;
import com.glq1218.util.BeanCopyUtils;
import com.glq1218.util.SecurityUtils;
import com.glq1218.util.ServiceUtils;
import com.glq1218.util.SortUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author glq1218
 * @description TODO
 * @since 2023/5/5 18:44
 */
@Service("reportService")
@RequiredArgsConstructor
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {
    private final UserMapper userMapper;
    private final ArticleMapper articleMapper;

    @Override
    public String addReport(AddReportBO addReportBO) {
        Report report = BeanCopyUtils.copyBean(addReportBO, Report.class);
        report.setActiveId(SecurityUtils.getUserId());
        boolean saved = save(report);
        if (saved) {
            return "举报成功";
        }
        throw new RuntimeException("系统异常");
    }

    @Override
    public PaginationResult<ReportBO> queryReportList(PaginationParam paginationParam, SortParam sortParam, IntervalStr intervalStr, QueryReportBO queryReportBO) {
        // 处理时间间隔
        Interval interval = ServiceUtils.getInterval(intervalStr);
        // 分页对象
        Page<Report> reportPage = ServiceUtils.getPage(paginationParam);
        // 构建查询条件对象
        String sortSql = "";
        if (StringUtils.hasText(sortParam.getSortField())) {
            sortSql = SortUtils.getSortSql(sortParam);
        }
        LambdaQueryWrapper<Report> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper
                .like(StringUtils.hasText(queryReportBO.getDescription()), Report::getDescription, queryReportBO.getDescription())
                .eq(queryReportBO.getStatus() != null, Report::getStatus, queryReportBO.getStatus())
                .between(StringUtils.hasText(intervalStr.getUpdatedAtStart()) && StringUtils.hasText(intervalStr.getUpdatedAtEnd()),
                        Report::getUpdatedAt, interval.getUpdatedAtStart(), interval.getUpdatedAtEnd())
                .between(StringUtils.hasText(intervalStr.getCreatedAtStart()) && StringUtils.hasText(intervalStr.getCreatedAtEnd()), Report::getCreatedAt, interval.getCreatedAtStart(), interval.getCreatedAtEnd())
                .last(StringUtils.hasText(sortParam.getSortField()) && StringUtils.hasText(sortParam.getSortOrder()), sortSql);
        // 分页查询
        page(reportPage, lambdaQueryWrapper);
        // 获取总数，如果总数为0，就直接返回
        long total = reportPage.getTotal();
        if (total <= 0L) {
            return PaginationResult.<ReportBO>builder().total(total).build();
        }
        // 将Report转为ReportBO
        reportPage.convert(report -> {
            ReportBO reportBO = BeanCopyUtils.copyBean(report, ReportBO.class);
            String passiveName = userMapper.getNicknameByUserId(report.getPassiveId());
            String activeName = userMapper.getNicknameByUserId(report.getActiveId());
            String articleName = articleMapper.selectById(report.getArticleId()).getName();
            reportBO.setPassiveName(passiveName);
            reportBO.setActiveName(activeName);
            reportBO.setArticleName(articleName);
            return reportBO;
        });
        List<Report> reportList = reportPage.getRecords();
        List<ReportBO> reportBOList = BeanCopyUtils.copyBeanList(reportList, ReportBO.class);
        return PaginationResult.<ReportBO>builder().total(total).records(reportBOList)
                .build();
    }
}
